home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / RTANKSRC.ZIP / INPUT.C < prev    next >
Text File  |  1989-02-09  |  3KB  |  136 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alloc.h>
  4. #include <string.h>
  5. #include <dtypes.h>
  6. #include <conio.h>
  7. #include <ctype.h>
  8. #include <nkeybd.h>
  9. #include "input.h"
  10.  
  11. #define BLOCK 22
  12.  
  13. static int x,y,l,maxlen;
  14. static char b[80];
  15.  
  16. /*
  17.  * Function : void _add_char(int ch)
  18.  * Purpose  : Append a character to buf
  19.  * Date     : 10/06/1988 22:40:56
  20.  */
  21. void _add_char(int ch)
  22. {
  23.    if (l<maxlen) {
  24.       b[l++]=ch;
  25.       b[l]=NULL;
  26.       gotoxy(x++,y); putch(ch);
  27.    } else putchar(7);
  28.  
  29. } /* void _add_char(int ch) */
  30.  
  31. /*
  32.  * Function : void _del_char(void)
  33.  * Purpose  : Deletes the last character in the buffer
  34.  * Date     : 10/06/1988 22:48:38
  35.  */
  36. void _del_char(void)
  37. {
  38.    if (l>0) {
  39.       b[--l]=NULL;
  40.       gotoxy(--x,y); putch(BLOCK);
  41.       gotoxy(x,y);
  42.    } else putchar(7);
  43. } /* void _del_char(void) */
  44.  
  45. /*
  46.  * Function : char *getline(int len, char *s)
  47.  * Purpose  : Gets a LEN length string from keyboard and stores it in S.
  48.  * Date     : 10/06/1988 20:06:03
  49.  */
  50. char *getline(int len, char *s)
  51. {
  52. int ox,oy,a;
  53. BOOL first;
  54. char ch;
  55.  
  56.    x=ox=wherex(); y=oy=wherey();
  57.    l=0; maxlen=len;
  58.    first=TRUE;
  59.  
  60.    gotoxy(x,y);
  61.    if (*s) cputs(s);
  62.    for (a=strlen(s); a<len; a++) putch(BLOCK);
  63.    gotoxy(ox,oy);
  64.  
  65.    do {
  66.       while (!kbhit())
  67.          ;
  68.       ch=getkey();
  69.  
  70.       if (isprint(ch)) {
  71.  
  72.          if (first) {
  73.             gotoxy(ox,oy);
  74.             for (a=0; a<len; a++) putch(BLOCK);
  75.             gotoxy(ox,oy);
  76.             first=FALSE;
  77.          }
  78.          _add_char(ch);
  79.  
  80.       } else {
  81.          switch(ch) {
  82.             case CR : if (first) {
  83.                          strcpy(b,s);
  84.                          x+=strlen(s);
  85.                       }
  86.  
  87.                       if (strlen(b) < len) {
  88.                          gotoxy(x,y);
  89.                          for (a=strlen(b); a<len; a++)
  90.                             putch(' ');
  91.                       }
  92.                       gotoxy(ox,oy);
  93.                       b[len]=NULL;
  94.                       break;
  95.  
  96.             case BS : _del_char();
  97.                       if (l==0) {
  98.                          gotoxy(ox,oy);
  99.                          if (*s) cputs(s);
  100.                          for (a=strlen(s); a<len; a++) putch(BLOCK);
  101.                          gotoxy(ox,oy);
  102.                          first=TRUE;
  103.                       }
  104.                       break;
  105.  
  106.             default : putchar(7); break;
  107.          }
  108.       } /* if not printable character */
  109.    } while (ch!=CR);
  110.  
  111.    strcpy(s,b);
  112.  
  113.    return(s);
  114. } /* char *getline(int len, char *s) */
  115.  
  116. /*
  117.  * Function : float get_float(int l, char *s)
  118.  * Purpose  : Returns a floating point number returned from keyboard
  119.  * Date     : 10/06/1988 20:04:11
  120.  */
  121. float get_float(int l, char *s)
  122. {
  123.    return(atof(getline(l,s)));
  124. } /* float get_float(int l, char *s) */
  125.  
  126. /*
  127.  * Function : int get_int(int l, char *s)
  128.  * Purpose  : Returns an integer from user keyboard input
  129.  * Date     : 10/06/1988 23:24:39
  130.  */
  131. int get_int(int l, char *s)
  132. {
  133.    return(atoi(getline(l,s)));
  134. } /* int get_int(int l, char *s) */
  135.  
  136.